#! /usr/bin/env python
# -*- coding: utf-8 -*-

"""
Расчет грида плотности распределения точек стандартным способом
"""

import sys
from optparse import OptionParser
from plane import Plane
from grid_plane import PlaneGrid
from math import sqrt

def line_parse(line):
    line = line.expandtabs(1)
    line = [x for x in line.split(' ') if x != '' ]    
    DIP, DIR = 0, 0    
    
    try:
        DIR, DIP = int(line.pop(0)), int(line.pop(0))
    except:
        #sys.stdout.write('WARNING! Cannot parse string: %s\n' % line)
        return None   
    line = Plane(DIR, DIP)
    return line

# Parsing of an options
optparser= OptionParser()
optparser.add_option('-n', '--cell size', dest='gsize',
                     help='size of a net cell',
                     default=10)

options, args = optparser.parse_args(sys.argv[1:])

lines = sys.stdin.read().split('\n')

# parsing of the data
data=[]
for line in lines:
    val = line_parse(line)
    if val != None: data.append(val)  

gsize = int(options.gsize)*2
# prepare an empty gird
density_grid = [[0] * gsize  for i in xrange(0, gsize)]
# finding the dinsity grid

mval = 0
for gx in range(0, gsize):
    for gy in range(0, gsize):
        for plane_ in data:            
            # distance between two planes
            true_x=gsize*(plane_.cos3+1)/2
            true_y=gsize*(1-plane_.cos1)/2
            dist = sqrt((gx - true_x)**2 + (gy - true_y)**2)            
            if dist < 3:
                density_grid[gx][gy] += 0.5
                mval = max(density_grid[gx][gy],mval)
            if dist < 2:
                density_grid[gx][gy] += 1
                mval = max(density_grid[gx][gy],mval)
            if dist < 1:
                density_grid[gx][gy] += 2
                mval = max(density_grid[gx][gy],mval)            
mval = mval*1.0
for gx in range(0, gsize):
    for gy in range(0, gsize):
        sys.stdout.write('%0.2f\t' % (density_grid[gx][gy]/mval))
    sys.stdout.write('\n')
